<?php
/**
 * Created by PhpStorm.
 * User: hisheng
 * Date: 2017/6/6
 * Time: 15:57
 */
namespace App\Http\Controllers\Api;

use App\Apply;
use App\Http\Controllers\Controller ;
use App\Loan;
use Illuminate\Support\Facades\Input;

class ApplyController extends Controller
{
    public $apply;
    public $loan;
    public function __construct(Apply $apply , Loan $loan)
    {
        $this->apply = $apply ;
        $this->loan = $loan;
    }
    
    public function index(){
        $user_id = Input::get('user_id',0);
        $apply = $this->apply->orderBy('updated_at','desc')->where('user_id',$user_id)->paginate(10);
        $applys = [];
        if($apply){
            $applys = $apply->toArray();
            $applys['data'] = $this->originIcon($applys['data']);
        }
        return $this->json($applys);
    }
    
    public function postAdd(){
        $this->insertDefault('loan_id');
        $this->insertDefault('user_id');
        $this->insertDefault('apply_time');
        $this->insertDefault('apply_amount');
        if($this->apply->loan_id){
            $loan = $this->loan->find($this->apply->loan_id);
            if($loan){
                $this->apply->rate_type = $loan->rate_type;
                $this->apply->loan_url = $loan->loan_url;
                $this->apply->name = $loan->name;
                $this->apply->icon = $loan->icon;
                $this->apply->phone = $loan->phone;
                $this->apply->save();
                $res = $this->apply->toArray();
                return $this->json($res);
            }
        }
        return $this->error('参数不存在');
        
    }
    
    public function insertDefault($name){
        $v = Input::get($name);
        if($name){
            $this->apply->$name = $v;
        }
    }
}

